package com.demo.my.Dao;

import com.demo.my.Entity.Attendance;
import com.demo.my.Entity.Attendance_Sheet;
import com.demo.my.Util.BaseDAO;
import com.demo.my.Util.DBUtil;

import javax.persistence.Table;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class AttendanceDao extends BaseDAO {

    public List<Attendance_Sheet> getAttendanceList() throws Exception {
        String sql="select b.id,a.name,b.employee_number,b.day,b.time_type,b.start_time,b.start_type,b.end_time,b.end_type,b.work_type,b.notes from employee as a right join attendance as b on a.employee_number=b.employee_number;";
        Object[] obj ={};
        return getList(sql, new Attendance_Sheet(), obj);
    }

    public List<Attendance_Sheet> getAttendanceList(Integer employeeNumber) throws Exception {
        String sql="select b.id,a.name,b.employee_number,b.day,b.time_type,b.start_time,b.start_type,b.end_time,b.end_type,b.work_type,b.notes from employee as a right join attendance as b on a.employee_number=b.employee_number where a.employee_number=?;";
        Object[] obj ={employeeNumber};
        return getList(sql, new Attendance_Sheet(), obj);
    }

    public void addAttendance(Attendance attendance) throws Exception {
        String sql="insert into attendance values(null,?,?,?,?,?,?,?,?,?)";
        Object[] obj ={attendance.getEmployeeNumber(),attendance.getDay(),attendance.getTimeType(),attendance.getStartTime(),
        attendance.getStartType(),attendance.getEndTime(),attendance.getEndType(),attendance.getWorkType(),attendance.getNotes()};
        addOrupdOrdel(sql,obj);
    }


    public Attendance getOne(String sql, Attendance o, Object... obj) throws Exception {
        BaseDAO dao = new BaseDAO();
        Attendance one = dao.getOne(sql, new Attendance(), obj);
        return one;
    }

    public void updateAttend(int aid,String status,String type,String time) throws SQLException {
        Connection connection = DBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement("update attendance set end_time=?,end_type=?,work_type=? where id=?;");
        ps.setObject(1,time);
        ps.setObject(2,type);
        ps.setObject(3,status);
        ps.setObject(4,aid);
        ps.execute();
        DBUtil.close(null,ps,connection);
    }
}
